package com.woniu108.extra.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu108.carService.commons.log.AdminHistoryLog;
import com.woniu108.carService.commos.dto.StreamDto;
import com.woniu108.carService.commos.exception.WoniuException;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.carService.commos.util.StaticData;
import com.woniu108.extra.client.ProductClient;
import com.woniu108.extra.model.Stream;
import com.woniu108.extra.model.StreamInfo;
import com.woniu108.extra.service.StreamInfoService;
import com.woniu108.extra.service.StreamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 流程表 前端控制器
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/stream")
@Api(tags = "整体流程接口")
public class StreamController {

    @Resource
    private StreamService streamService;
    @Resource
    private ProductClient productClient;
    @Resource
    private StreamInfoService streamInfoService;

    /**
     * 新增流程
     * @param token
     * @param streamName 流程名称
     * @return result 是否正常访问接口结果
     */
    @GetMapping("/addStream.api")
    @ApiOperation("新增流程")
    @AdminHistoryLog
    public Result<Stream> addStream(@RequestHeader String token,String streamName){
        QueryWrapper<Stream> wrapper = new QueryWrapper<>();
        wrapper.eq("stream_name",streamName);
        Stream stream = streamService.getOne(wrapper);
        if(ObjectUtil.isNotEmpty(stream)&&stream.getStreamState()!= StaticData.STREAM_STATE_DELETE){
            throw new WoniuException("该流程名称已被使用",10001);
        }
        Stream stream1 = new Stream();
        stream1.setStreamName(streamName);
        stream1.setStreamState(StaticData.STREAM_STATE_NORMAL);
        streamService.save(stream1);
        System.out.println("新增的流程ID:"+stream1.getStreamId());
        return Result.getResult(Stream.class).setMsg("新增流程成功").setData(stream1);
    }

    /**
     * 删除流程及流程下的所有详细信息
     * @param token
     * @param streamId 流程Id
     * @return result 是否正常访问接口结果
     */
    @GetMapping("/deleteStream.api")
    @ApiOperation("删除流程")
    @AdminHistoryLog
    @ApiImplicitParam(name = "streamId",value = "流程Id")
    public Result deleteStream(@RequestHeader String token,Integer streamId){
        Stream stream = streamService.getById(streamId);
        if(ObjectUtil.isEmpty(stream)||stream.getStreamState()==StaticData.STREAM_STATE_DELETE) throw new WoniuException("流程状态异常",10001);
        //判断是否有服务项设置使用此流程,占用则不可删除
        Result<Boolean> result = productClient.findServiceInfoByStreamId(streamId);
        if(result.getCode()!=200) throw new WoniuException(result.getMsg(),result.getCode());
        stream.setStreamState(StaticData.STREAM_STATE_DELETE);
        streamService.updateById(stream);
        //删除流程详细信息
        QueryWrapper<StreamInfo> wrapper = new QueryWrapper<StreamInfo>();
        wrapper.eq("stream_id",streamId);
        streamInfoService.remove(wrapper);
        return Result.getResult().setMsg("删除流程成功");
    }

    /**
     * 根据流程状态获取所有流程信息
     * @param streamState 流程状态
     * @return List<StreamDto> 流程信息列表
     */
    @GetMapping("/streamList.api")
    @ApiOperation("根据流程状态获取所有流程信息")
    @ApiImplicitParam(name = "streamState",value = "流程状态")
    public Result<List<StreamDto>> streamList(Integer streamState){
        List<Stream> streamList=new ArrayList<>();
        if(streamState!=null) {
            QueryWrapper<Stream> wrapper = new QueryWrapper<>();
            wrapper.eq("stream_state",streamState);
            streamList=streamService.list(wrapper);
        }else{
            streamList = streamService.list();
        }
        List<StreamDto> dtos = new ArrayList<>();
        for (Stream stream : streamList) {
            StreamDto dto = BeanUtil.toBean(stream, StreamDto.class);
            dtos.add(dto);
        }
        return Result.newListInstance(StreamDto.class).setData(dtos);
    }

    /**
     * 禁用流程
     * @param streamId
     * @return result 是否正常访问接口结果
     */
    @GetMapping("/disableStream.api")
    @ApiImplicitParam(name = "streamId",value = "流程Id")
    @ApiOperation("禁用流程")
    @AdminHistoryLog
    public Result disableStream(Integer streamId){
        Stream stream = streamService.getById(streamId);
        if(ObjectUtil.isEmpty(stream)) throw new WoniuException("流程信息不存在",10001);
        //判断是否有服务项设置使用此流程,占用则不可禁用
        Result<Boolean> result = productClient.findServiceInfoByStreamId(streamId);
        if(result.getCode()!=200) throw new WoniuException(result.getMsg(),result.getCode());
        if(stream.getStreamState()!=StaticData.STREAM_STATE_NORMAL) throw new WoniuException("流程信息状态异常",10001);
        stream.setStreamState(StaticData.STREAM_STATE_DISABLED);
        streamService.updateById(stream);
        return Result.getResult().setMsg("禁用流程成功");
    }

    /**
     * 启用流程
     * @param streamId
     * @return result 是否正常访问接口结果
     */
    @GetMapping("/ableStream.api")
    @ApiImplicitParam(name = "streamId",value = "流程Id")
    @ApiOperation("启用流程")
    @AdminHistoryLog
    public Result ableStream(Integer streamId){
        Stream stream = streamService.getById(streamId);
        if(ObjectUtil.isEmpty(stream)) throw new WoniuException("流程信息不存在",10001);
        if(stream.getStreamState()!=StaticData.STREAM_STATE_DISABLED) throw new WoniuException("流程信息状态异常",10001);
        stream.setStreamState(StaticData.STREAM_STATE_NORMAL);
        streamService.updateById(stream);
        return Result.getResult().setMsg("禁用流程成功");
    }

    /**
     * 修改流程名称
     * @param streamId
     * @param streamName
     * @return result 是否正常访问接口结果
     */
    @GetMapping("/changeStreamName.api")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "streamId",value = "流程Id"),
            @ApiImplicitParam(name = "streamName",value = "流程名称")
    })
    @ApiOperation("修改流程名称")
    @AdminHistoryLog
    public Result changStreamName(Integer streamId,String streamName){
        Stream stream = streamService.getById(streamId);
        if(ObjectUtil.isEmpty(stream)) throw new WoniuException("流程信息不存在",10001);
        if(stream.getStreamState()==StaticData.STREAM_STATE_DELETE) throw new WoniuException("流程状态异常",10001);
        stream.setStreamName(streamName);
        streamService.updateById(stream);
        return Result.getResult().setMsg("修改流程名称成功");
    }
}

